package cc.bluemc.platform.annotation.fieldFormart;

import cc.bluemc.platform.annotation.AnnotationExecuterInterface;
import cc.bluemc.platform.springUtil.SpringConfigUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class FieldFormartExecter implements AnnotationExecuterInterface{
	private Log log = LogFactory.getLog(this.getClass());
	@Override
	public <T> T execute(Annotation annotation, T value) {
		FieldFormart formart = (FieldFormart) annotation;
		try {
			if(Void.class==formart.clazz()){
				String format =formart.value();
				JSONObject jObject = JSON.parseObject(format);
				String result= (String) jObject.get(String.valueOf(value));
				return (T) result;
			}else{
				Class<?> clazz = formart.clazz();
				String methodName = formart.method();
				Class<?>[] paramsType = formart.paramsType();
				Method method = clazz.getMethod(methodName,paramsType);
				//XXX 当使用 o=clazz.newInstance()时，spring 没有注入
				Object o = SpringConfigUtil.getBean(StringUtils.uncapitalize(clazz.getSimpleName()));
				Object result = method.invoke(o, value);
				return (T) result;
			}
		} catch (Exception e) {
			log.error("字段格式化错误",e);
		}
		return null;
	}
	
	

}
